package com.cy.pj.sys.service.impl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.pojo.PageObject;
import com.cy.pj.sys.dao.DaoHouse;
import com.cy.pj.sys.pojo.HouseUser;
import com.cy.pj.sys.pojo.SysHouse;
import com.cy.pj.sys.service.ServiceHouse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ServiceHouseImpl implements ServiceHouse {
    @Autowired
    private DaoHouse daoHouse;

    @Override
    public PageObject findHouses(String room, String area, Integer price, String place,
                                     String state, Integer pageCurrent) {
        //对参数进行校验
        if(pageCurrent==null||pageCurrent<1)
            throw new IllegalArgumentException("页码值不合法");
        //基于查询条件查询总记录数
        int rowCount = daoHouse.getRowCount(room, area, price, place, state);
        if(rowCount==0)
            throw new ServiceException("没有找到对应记录");
        //查询当前页记录
        int pageSize=5;//页面大小
        int startIndex=(pageCurrent-1)*pageSize;
        List<HouseUser> records = daoHouse.findHouses(room, area, price, place, state, startIndex, pageSize);
        PageObject<HouseUser> sysHousePageObject = new PageObject<>(rowCount, records, pageSize, pageCurrent);
        return sysHousePageObject;
    }


    //根据id或userid查询房屋信息
    @Override
    public List<SysHouse> findHouseById(Integer id, Integer user_id) {
        List<SysHouse> houseById = daoHouse.findHouseById(id, user_id);
        return houseById;
    }

    //基于id 删除房屋信息
    @Override
    public int deleteHouse(Integer id) {
        return daoHouse.deleteHouse(id);
    }


}
